package com.nurt.controller.order;

import com.nurt.service.OrderService;
import com.nurt.service.impl.OrderServiceImpl;
import com.nurt.utils.ReturnCode;
import com.nurt.utils.ServletHelper;
import com.alibaba.fastjson2.JSONObject;
import com.nurt.dto.ResultDto;
import com.nurt.entity.OrderEntity;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = {"/orders/*"})
public class OrderController extends HttpServlet {

    private OrderService orderService;

    public void init() {
        orderService = new OrderServiceImpl();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        String[] pathParts = pathInfo.split("/");
        String userId = pathParts[1];
        String status = request.getParameter("status");

        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        
        List<OrderEntity> orders = null;
        if (status != null) {
            orders = orderService.getOrdersByStatus(Integer.parseInt(userId), Integer.parseInt(status));
        } else {
            orders = orderService.getOrders(Integer.parseInt(userId));
        }
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().write(ResultDto.success(orders));
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        JSONObject jsonObject = ServletHelper.getRequestBodyAsJson(request);
        int userId = jsonObject.getIntValue("userId");
        OrderEntity order = new OrderEntity();
        order.setUserId(userId);
        order.setStatus(1);
        int orderId = orderService.createOrder(order);
        
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        if (orderId == -1) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().write(ResultDto.fail(ReturnCode.INTERNAL_SERVER_ERROR.getCode() , "创建订单失败"));
            return;
        }
        
        order.setOrderId(orderId);
        response.getWriter().write(ResultDto.success(order));
    }

    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        String[] pathParts = pathInfo.split("/");
        String orderId = pathParts[1];
        JSONObject jsonObject = ServletHelper.getRequestBodyAsJson(request);
        int status = jsonObject.getIntValue("status");
        orderService.updateOrderStatus(Integer.parseInt(orderId), status);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(ResultDto.success(null));
    }
}
